﻿using MaterialManagement.API.Application.Command.ERP;
using MaterialManagement.Domain.ERP;
using MaterialManagement.ErrorCode;
using MaterialManagement.Infrastructure.Interface;
using MediatR;

namespace MaterialManagement.API.Application.Handler.ERP
{
    public class GetMaterialInfoHandler : IRequestHandler<GetMaterialInfoCommand, APIResult<List<MaterialInfoModel>>>
    {
        private readonly IBaseRepository<MaterialInfoModel> materialInfoRepo;

        public GetMaterialInfoHandler(IBaseRepository<MaterialInfoModel> materialInfoRepo)
        {
            this.materialInfoRepo = materialInfoRepo;
        }
        public Task<APIResult<List<MaterialInfoModel>>> Handle(GetMaterialInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<MaterialInfoModel>> result = new APIResult<List<MaterialInfoModel>>();
            var res = materialInfoRepo.GetAll().Where(x=>x.IsDel == false);
            if (!string.IsNullOrEmpty(request.MaterialCode))
            {
                res = res.Where(x=>x.MaterialCode.Contains(request.MaterialCode));
            }
            result.Message = "显示成功";
            result.Data = res.ToList();
            result.Code = APIEnum.Ok;
            return Task.FromResult(result);
        }
    }
}
